package com.heima.search.service.impl;

import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.AppThreadLocalUtil;
import com.heima.search.dto.ApArticleSearchDto;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.service.IApUserSearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements IApUserSearchService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Async
    public void saveRecord(ApUserSearch userSearch) {
        //保存用户的搜索记录
        //查询当前用户的关键字是否已经存在
        Query query = new Query();
        query.addCriteria(Criteria.where("keyword").is(userSearch.getKeyword()));
        if (userSearch.getUserId() != null){
            query.addCriteria(Criteria.where("userId").is(userSearch.getUserId()));
        }else {
            query.addCriteria(Criteria.where("equipmentId").is(userSearch.getEquipmentId()));
        }
        ApUserSearch one = mongoTemplate.findOne(query, ApUserSearch.class);
        //不存在记录就新增
        if (one == null){
            mongoTemplate.save(userSearch);
        }
    }

    /**
     * 加载用户搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ApArticleSearchDto dto) {
        //根据前端传递的分页来查询
        Query query = new Query();
        User user = AppThreadLocalUtil.get();
        if (user == null){
            query.addCriteria(Criteria.where("equipmentId").is(dto.getEquipmentId()));
        }else {
            query.addCriteria(Criteria.where("userId").is(user.getUserId()));
        }
        Pageable page = PageRequest.of(0,dto.getSize(), Sort.Direction.DESC,"createdTime");
        query.with(page);
        List<ApUserSearch> list = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(list);
    }

    /**
     * 删除搜索记录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult del(ApArticleSearchDto dto) {
        //获取id集合 根据id集合删除搜索记录
        List<ApUserSearch> hisList = dto.getHisList();
        List<String> ids = new ArrayList<>();
        for (ApUserSearch userSearch : hisList) {
            ids.add(userSearch.getId());
        }
        //根据id集合删除搜索记录
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").in(ids));
        mongoTemplate.remove(query,ApUserSearch.class);
        return ResponseResult.okResult();
    }
}
